#include <drv_common.h>
#include "dev_oled.h"
#include "drv_soft_i2c.h"

struct rt_soft_i2c_drv *oled_i2c_drv;

#define OLED_ADDRESS                    0x3c
#define OLED_CMD_ADDRESS                0x00
#define OLED_DATA_ADDRESS               0x40

const unsigned char F8X16[]=
{
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
  0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,// ! 1
  0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// " 2
  0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,// # 3
  0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,// $ 4
  0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,// % 5
  0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,// & 6
  0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// ' 7
  0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,// ( 8
  0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,// ) 9
  0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,// * 10
  0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,// + 11
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,// , 12
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,// - 13
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,// . 14
  0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,// / 15
  0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,// 0 16
  0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// 1 17
  0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,// 2 18
  0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,// 3 19
  0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,// 4 20
  0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,// 5 21
  0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,// 6 22
  0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,// 7 23
  0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,// 8 24
  0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,// 9 25
  0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,// : 26
  0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,// ; 27
  0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,// < 28
  0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,// = 29
  0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,// > 30
  0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,// ? 31
  0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,// @ 32
  0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,// A 33
  0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,// B 34
  0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,// C 35
  0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,// D 36
  0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,// E 37
  0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,// F 38
  0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,// G 39
  0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,// H 40
  0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// I 41
  0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,// J 42
  0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,// K 43
  0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,// L 44
  0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,// M 45
  0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,// N 46
  0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,// O 47
  0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,// P 48
  0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,// Q 49
  0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,// R 50
  0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,// S 51
  0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,// T 52
  0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,// U 53
  0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,// V 54
  0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,// W 55
  0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,// X 56
  0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,// Y 57
  0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,// Z 58
  0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,// [ 59
  0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,// \ 60
  0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,// ] 61
  0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// ^ 62
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,// _ 63
  0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// ` 64
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,// a 65
  0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,// b 66
  0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,// c 67
  0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,// d 68
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,// e 69
  0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// f 70
  0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,// g 71
  0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,// h 72
  0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// i 73
  0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,// j 74
  0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,// k 75
  0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,// l 76
  0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,// m 77
  0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,// n 78
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,// o 79
  0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,// p 80
  0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,// q 81
  0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,// r 82
  0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,// s 83
  0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,// t 84
  0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,// u 85
  0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,// v 86
  0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,// w 87
  0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,// x 88
  0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,// y 89
  0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,// z 90
  0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,// { 91
  0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,// | 92
  0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,// } 93
  0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// ~ 94

};

unsigned char BMP[] =
{
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xE0,0xE0,0xF0,
    0x10,0x30,0x18,0x18,0x18,0x08,0x18,0x08,0x18,0x18,0x18,0x18,0x18,0x18,0x10,0x10,
    0x10,0x30,0x30,0x10,0xF0,0xF0,0xE0,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0xE0,0xF0,0xF0,0xF0,0xF0,0xF0,0xE0,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0xC0,0xC0,0x00,0x00,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0xE0,0xE0,0xF8,0xFC,0x36,0x3A,0x1F,0x1F,0xC7,0x63,0x30,0x16,0x07,
    0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x03,0x93,0xD7,0x72,0x62,0xC6,0x1C,0xF0,
    0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0x3E,0xD8,0xD8,0xD8,0xF8,0xF8,
    0x38,0xFF,0xFF,0x30,0x30,0x30,0x30,0xF0,0xF0,0xE0,0x60,0x60,0x00,0x70,0x78,0x3C,
    0xFE,0xFF,0x07,0x03,0x00,0x00,0x00,0x60,0xF0,0xFE,0x1F,0x07,0x01,0x00,0x00,0x00,
    0x00,0x00,0x00,0xFC,0xFF,0xFF,0x03,0x07,0x07,0x0E,0x1E,0xFC,0xF0,0x00,0x00,0xFF,
    0xFF,0x03,0x00,0x03,0x1F,0x3F,0x3F,0x78,0xE0,0xE0,0x78,0x3E,0x0F,0x07,0x01,0x00,
    0x00,0xF8,0x0E,0x3F,0x21,0x70,0x5D,0x5C,0xC0,0xC0,0xC0,0xC1,0x80,0x80,0x8C,0x86,
    0x87,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x80,0x80,0x80,0x80,0x80,0x8C,0xBC,0xB8,0xC0,0xC1,0x47,0x46,0x44,0x60,0x20,
    0x3B,0x1B,0x0F,0xFC,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x3F,0x7F,0x7F,0x39,0x39,
    0x7F,0x7F,0x7F,0x0C,0x7C,0x7C,0x7E,0x3F,0x77,0x60,0x20,0x20,0x00,0x00,0x00,0x00,
    0x0F,0x7F,0x7F,0x03,0x03,0x03,0x33,0x73,0xFF,0xFF,0x7F,0x03,0x03,0x03,0x03,0x03,
    0x03,0x03,0x0C,0x1F,0x0F,0x0C,0x0C,0x0E,0x0E,0x06,0x07,0x03,0x01,0x00,0x00,0x3F,
    0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
    0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0xF8,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x07,0x3C,0x60,0xE0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xE0,0x30,
    0x1C,0x06,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x80,0x80,0xC0,0xC0,0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xF0,0xF0,
    0xF0,0xF0,0xF0,0x78,0x78,0x7C,0x7C,0x7C,0x3C,0x3E,0x3E,0x1E,0x1E,0x1F,0x1F,0x0E,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x06,0x04,0x04,0x0C,0x08,0x18,0x70,0xF0,
    0xA0,0x20,0x20,0x20,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x20,0x20,0x20,0x20,
    0x20,0x20,0x20,0xB0,0xB0,0xF8,0x78,0x0C,0x0C,0x04,0x06,0x03,0x01,0x01,0x00,0x00,
    0x00,0x00,0x00,0x00,0x0F,0x0F,0x0F,0x8F,0x8F,0x8E,0xDE,0xDE,0xDE,0xFE,0xFE,0xFC,
    0xFC,0xFC,0xFC,0xFC,0xF8,0x78,0x78,0x78,0xFC,0xFC,0xFC,0xFC,0xFC,0xFE,0xFE,0xFE,
    0xEF,0xEF,0xEF,0xCF,0xC7,0xC7,0xC7,0xC7,0x83,0x83,0x83,0x83,0x81,0x81,0x81,0x01,
    0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x01,0x03,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
    0x02,0x02,0x03,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x07,0x07,0x07,0x07,0x07,0x03,0x03,0x03,0x03,
    0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,
    0x01,0x03,0x03,0x03,0x03,0x03,0x07,0x07,0x07,0x07,0x07,0x1F,0x1F,0x1F,0x1F,0x1F,
    0x1F,0x1F,0x1F,0x3E,0x3E,0x3E,0x3E,0x3E,0x3C,0x7C,0x7C,0xFC,0xFC,0xFC,0x78,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};

static rt_err_t rt_oled_write_reg(struct rt_soft_i2c_drv *dev, rt_uint8_t reg, rt_uint8_t data)
{
    rt_uint8_t buf[2];
    struct rt_soft_i2c_msg msgs;

    buf[0] = reg;
    buf[1] = data;

    msgs.addr = OLED_ADDRESS;
    msgs.flags = RT_SOFT_I2C_WR;
    msgs.buf = buf;
    msgs.len = 2;

    /* 调用I2C设备接口传输数据 */
    rt_soft_i2c_master_xfer(dev, &msgs, 1);

    return RT_EOK;
}

rt_err_t rt_oled_read_regs(struct rt_soft_i2c_drv *dev, rt_uint8_t len, rt_uint8_t *buf)
{
    struct rt_soft_i2c_msg msgs;

    msgs.addr = OLED_ADDRESS;
    msgs.flags = RT_SOFT_I2C_RD;
    msgs.buf = buf;
    msgs.len = len;

    /* 调用I2C设备接口传输数据 */
    rt_soft_i2c_master_xfer(dev, &msgs, 1);
    return RT_EOK;
}

static void rt_oled_write_cmd(rt_uint8_t cmd)
{
    if(oled_i2c_drv == RT_NULL)
    {
        rt_kprintf("find soft i2c1 fail\n");
        return;
    }
    rt_oled_write_reg(oled_i2c_drv, OLED_CMD_ADDRESS, cmd);
}

static void rt_oled_write_data(rt_uint8_t data)
{
    if(oled_i2c_drv == RT_NULL)
    {
        rt_kprintf("find soft i2c1 fail\n");
        return;
    }
    rt_oled_write_reg(oled_i2c_drv, OLED_DATA_ADDRESS, data);
}

static void rt_oled_set_pos(rt_uint8_t x, rt_uint8_t y)
{
    rt_oled_write_cmd(0xb0 + y);
    rt_oled_write_cmd(((x & 0xf0) >> 4) | 0x10);
    rt_oled_write_cmd((x & 0x0f) | 0x01);
}

void rt_oled_show_str(rt_uint8_t x, rt_uint8_t y, char *str)
{
    rt_uint8_t c = 0,i = 0,j = 0;

    while(str[j] != '\0')
    {
        c = str[j] - 32;
        if(x > 120)
        {
            x = 0;
            y++;
        }
        rt_oled_set_pos(x, y);
        for(i = 0; i < 8; i++)
        {
            rt_oled_write_data(F8X16[c * 16 + i]);
        }
        rt_oled_set_pos(x, y + 1);
        for(i = 0; i < 8; i++)
        {
            rt_oled_write_data(F8X16[c * 16 + i + 8]);
        }
        x += 8;
        j++;
    }
}

void rt_oled_show_digit(rt_uint8_t x, rt_uint8_t y, int digit)
{
    char ch[6];
    int i = 1;
    if(digit < 0)
    {
        ch[0] = 45;
        digit = 0 - digit;
    }
    else
    {
        ch[0] = (digit % 1000000 / 100000) + 48;
        if(ch[0] == '0')
        {
            ch[0] = ' ';
        }
    }

    ch[1] = (digit % 100000 / 10000) + 48;
    ch[2] = (digit % 10000 / 1000) + 48;
    ch[3] = (digit % 1000 / 100) + 48;
    ch[4] = (digit % 100 / 10) + 48;
    ch[5] = (digit % 10) + 48;
    while(ch[i] == '0' && i < 5)
    {
        ch[i] = ' ';
        i++;
    }
    rt_oled_show_str(x,y,ch);
}

void rt_oled_show_bmp(rt_uint8_t x1, rt_uint8_t y1,
                      rt_uint8_t x2, rt_uint8_t y2,
                      rt_uint8_t BMP[])
{
    rt_uint32_t j=0;
    rt_uint8_t x, y;
    if(y2 % 8 == 0)
    {
        y = y2 / 8;
    }
    else
    {
        y = y2 / 8 + 1;
    }
    for(y = y1; y < y2; y++)
    {
        rt_oled_set_pos(x1, y);
        for(x = x1; x < x2; x++)
        {
            rt_oled_write_data(BMP[j++]);
        }
    }
}

void rt_oled_fill(rt_uint8_t fill_data)
{
    rt_uint8_t m,n;

    if(oled_i2c_drv == RT_NULL)
    {
        rt_kprintf("find soft i2c1 fail\n");
        return;
    }

    for(m = 0; m < 8; m++)
    {
        rt_oled_write_cmd(0xb0 + m);
        rt_oled_write_cmd(0x00);
        rt_oled_write_cmd(0x10);
        for(n=0;n<128;n++)
        {
            rt_oled_write_data(fill_data);
        }
    }
}

int rt_oled_dev_init(void)
{
    oled_i2c_drv = rt_find_soft_i2c("soft_i2c1");
    if(oled_i2c_drv == RT_NULL)
    {
        rt_kprintf("find soft i2c1 fail\n");
        return RT_ERROR;
    }

    rt_oled_write_cmd(0xAE);
    rt_oled_write_cmd(0x20);
    rt_oled_write_cmd(0x10);
    rt_oled_write_cmd(0xb0);
    rt_oled_write_cmd(0xc8);
    rt_oled_write_cmd(0x00);
    rt_oled_write_cmd(0x10);
    rt_oled_write_cmd(0x40);
    rt_oled_write_cmd(0x81);
    rt_oled_write_cmd(0xff);
    rt_oled_write_cmd(0xa1);
    rt_oled_write_cmd(0xa6);
    rt_oled_write_cmd(0xa8);
    rt_oled_write_cmd(0x3F);
    rt_oled_write_cmd(0xa4);
    rt_oled_write_cmd(0xd3);
    rt_oled_write_cmd(0x00);
    rt_oled_write_cmd(0xd5);
    rt_oled_write_cmd(0xf0);
    rt_oled_write_cmd(0xd9);
    rt_oled_write_cmd(0x22);
    rt_oled_write_cmd(0xda);
    rt_oled_write_cmd(0x12);
    rt_oled_write_cmd(0xdb);
    rt_oled_write_cmd(0x20);
    rt_oled_write_cmd(0x8d);
    rt_oled_write_cmd(0x14);
    rt_oled_write_cmd(0xaf);

    rt_oled_fill(OLED_FILL_BLACK);

    rt_oled_set_pos(0, 0);
    rt_oled_show_bmp(0, 0, 128, 8, BMP);

    return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_oled_dev_init);



